package com.live.webvideo.project.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.live.webvideo.common.base.BaseController;
import com.live.webvideo.common.util.ApiResult;
import com.live.webvideo.common.util.Constant;
import com.live.webvideo.common.util.RdPage;
import com.live.webvideo.common.util.ToolUtil;
import com.live.webvideo.project.entity.RoomInfo;
import com.live.webvideo.project.service.RoomInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangdong
 * @date 2018/11/1 21:03
 * 描述：
 */
@Api(description = "视频首页接口", value = "用户首页接口")
@Scope("prototype")
@RestController
//@Controller
@EnableAutoConfiguration
public class RoomInfoController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(RoomInfoController.class);

    @Autowired
    private RoomInfoService roomInfoService;

    /**
     * 视频列表
     *
     * @return
     */
    @ApiOperation(value = "视频列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "current", value = "当前页", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页显示条数", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/queryRoomInfoList", method = RequestMethod.POST)
    public ApiResult<List<RoomInfo>> queryRoomInfoList(@RequestParam(value = "current", required = true) String current,
                                                         @RequestParam(value = "pageSize", required = true) String pageSize) {
        ApiResult<List<RoomInfo>> result = null;
        Map<String, Object> map = new HashMap<>();
        try {
            PageHelper.startPage(Integer.valueOf(current), Integer.valueOf(pageSize));

            List<RoomInfo> list = roomInfoService.selectRoomLists();

            PageInfo<RoomInfo> pageInfo = new PageInfo<>(list);

            RdPage rdPage = ToolUtil.getPage(pageInfo, Integer.valueOf(current), Integer.valueOf(pageSize));

            map.put("list", list);

            result = new ApiResult<>(Constant.SUCCEED_CODE_VALUE, Constant.OPERATION_SUCCESS, list, rdPage);

        } catch (Exception e) {
            result = new ApiResult<>(Constant.FAIL_CODE_VALUE, Constant.OPERATION_FAIL, null, null);
            logger.error("视频列表出错：" + e);
        }

        return result;
    }

    /**
     * 直播视频详情
     *
     * @return
     */
    @ApiOperation(value = "直播视频详情", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "直播id", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/detailRoomInfo", method = RequestMethod.POST)
    public ApiResult<RoomInfo> detailRoomInfo(@RequestParam(value = "id", required = true) String id) {
        ApiResult<RoomInfo> result = null;
        try {
            RoomInfo roomInfo = roomInfoService.selectRoomInfoById(Integer.valueOf(id.substring(3)));

            result = new ApiResult<>(Constant.SUCCEED_CODE_VALUE, Constant.OPERATION_SUCCESS, roomInfo, null);

        } catch (Exception e) {
            result = new ApiResult<>(Constant.FAIL_CODE_VALUE, Constant.OPERATION_FAIL, null, null);
            logger.error("直播视频详情出错：" + e);
        }

        return result;
    }
}
